package factory.one.controller;

import factory.one.entity.SysColumn;
import factory.one.entity.SysTable;
import factory.one.service.DatabaseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    private final DatabaseService databaseService;

    public AdminController(DatabaseService databaseService) {
        this.databaseService = databaseService;
    }

    @GetMapping("/index.html")
    public String adminIndex(Model model) {
        return "admin/index";
    }

    @GetMapping("/schema.html")
    private String schemaHtml(Model model){
        List<SysTable> userTables = databaseService.getUserTables();
        model.addAttribute("tables", userTables);

        Map<String,List<SysColumn>> map = new HashMap<>();
        for (SysTable t : userTables) {
            map.put(t.getName(),databaseService.descTable(t.getId()));
        }
        model.addAttribute("map", map);
        return "admin/schema";
    }
}
